/*
 * Copyright 2011 Philippe Blanc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package me.l1k3.fx.client.channel.impl;

import me.l1k3.core.client.Core;
import me.l1k3.fx.client.channel.inter.ShowAndHide;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style;

public final class ShowAndHideImplHeight implements ShowAndHide {
    private String position;
    private String top;
    private String left;
    private String width;
    private String height;
    
    @Override
    public final void init(Element element) {
        if("none".equals(Core.getComputedStyleProperty(element, "display"))) {
            Style style = element.getStyle();
            hide(style);
            style.setProperty("display", "block");
        }
    }
    
    @Override
    public final boolean isVisible(Style style) {
        return !style.getProperty("visibility").equals("hidden");
    }
    
    @Override
    public final void hide(Style style) {
        if(!style.getProperty("visibility").equals("hidden")) {
            position = style.getProperty("position");
            top = style.getProperty("top");
            left = style.getProperty("left");
            width = style.getProperty("width");
            height = style.getProperty("height");
            
            style.setProperty("position", "absolute");
            style.setProperty("top", "-100000px");
            style.setProperty("left", "-100000px");
            style.setProperty("width", "0px");
            style.setProperty("height", "0px");
            
            style.setProperty("visibility", "hidden");
        }
    }

    @Override
    public final void show(Style style) {
        if(style.getProperty("visibility").equals("hidden")) {
            style.setProperty("position", position);
            style.setProperty("top", top);
            style.setProperty("left", left);
            style.setProperty("width", width);
            style.setProperty("height", height);
            
            style.setProperty("visibility", "visible");
        }
    }
}
